package com.zlc.platform.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.common.service.AutowiredService;
import com.zlc.common.core.sys.model.entity.MayanPlatDic;
import com.zlc.common.core.sys.model.vo.MayanPlatDicVo;
import com.zlc.common.model.sys.DicPageRequest;
import com.zlc.common.util.AssertUtils;
import com.zlc.common.util.BeanCopyUtils;
import com.zlc.platform.model.DicEntity;
import com.zlc.platform.model.DicItemVo;
import com.zlc.platform.service.DicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.zlc.common.common.constant.PlatConstant.Status.ACTIVE;


@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class DicServiceImpl extends AutowiredService implements DicService {

    @Override
    public PageVo<MayanPlatDicVo> queryDicList(DicPageRequest pageRequest) {
        if (ObjectUtils.isEmpty(pageRequest.getChildFlag())) {
            pageRequest.setChildFlag("N");
        }
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<MayanPlatDicVo> list = mayanPlatDicMapper.queryList(pageRequest);
        return PageVo.page(list);
    }

    @Override
    public List<DicEntity> queryDics(List<String> codes) {
        LambdaQueryWrapper<MayanPlatDic> wrapper = mayanPlatDicService.wrapper();
        wrapper.in(MayanPlatDic::getParentCode,codes);
        List<MayanPlatDicVo> dicVos = mayanPlatDicService.queryVoList(wrapper);
        dicVos.forEach(d->d.setOrder(d.getOrder() == null ? 0 : d.getOrder()));
        if (!ObjectUtils.isEmpty(dicVos)) {
            return dicVos.stream().collect(Collectors.groupingBy(MayanPlatDicVo::getParentCode))
                    .entrySet()
                    .stream().flatMap(map->{
                        DicEntity entity = new DicEntity();
                        String parentCode = map.getKey();
                        List<MayanPlatDicVo> value = map.getValue();
                        if(!ObjectUtils.isEmpty(value)){
                            List<DicItemVo> dicItemVos = BeanCopyUtils.copyObjList(value, DicItemVo.class);
                            dicItemVos.sort(Comparator.comparing(DicItemVo::getOrder));
                            entity.setDicList(dicItemVos);
                        }
                        entity.setCode(parentCode);
                        return Stream.of(entity);
                    }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public void addDic(MayanPlatDicVo dicVo) {
        String code = dicVo.getCode();
        String parentCode = dicVo.getParentCode();
        AssertUtils.isTrue(!ObjectUtils.isEmpty(code),"请输入字典code");
        MayanPlatDicVo oldDic = mayanPlatDicService.getByCode(code);
        AssertUtils.isTrue(ObjectUtils.isEmpty(oldDic),"字典已存在");
        if(!ObjectUtils.isEmpty(parentCode)){
            MayanPlatDicVo parentDic = mayanPlatDicService.getByCode(parentCode);
            AssertUtils.isTrue(!ObjectUtils.isEmpty(parentDic),"父字典不存在");
            AssertUtils.isTrue(!Boolean.TRUE.equals(parentDic.getSystemFlag()),"系统级别字典不可编辑");
        }
        dicVo.setStatus(ACTIVE);
        mayanPlatDicService.saveVo(dicVo);
    }

    @Override
    public void updateDic(MayanPlatDicVo dicVo) {
        String code = dicVo.getCode();
        MayanPlatDicVo oldDic = mayanPlatDicService.getByCode(code);
        if(!ObjectUtils.isEmpty(oldDic)){
            AssertUtils.isTrue(!Boolean.TRUE.equals(oldDic.getSystemFlag()),"系统级别字典不可编辑");
        }
        mayanPlatDicService.saveVo(dicVo);
    }

    @Override
    public void changeDicStatus(MayanPlatDicVo dicVo) {
        String code = dicVo.getCode();
        MayanPlatDicVo oldDic = mayanPlatDicService.getByCode(code);
        if(!ObjectUtils.isEmpty(oldDic) && !ObjectUtils.isEmpty(dicVo.getStatus())){
            oldDic.setStatus(dicVo.getStatus());
            mayanPlatDicService.saveVo(oldDic);
        }
    }

    @Override
    public void removeDic(String id) {
        MayanPlatDic platDic = mayanPlatDicService.getById(id);
        if (!ObjectUtils.isEmpty(platDic)) {
            List<MayanPlatDicVo> childDics = mayanPlatDicService.getByParentCode(platDic.getCode());
            AssertUtils.isTrue(ObjectUtils.isEmpty(childDics),"存在子字典数据，不可删除");
            AssertUtils.isTrue(!Boolean.TRUE.equals(platDic.getSystemFlag()),"系统级别字典不可删除");
            mayanPlatDicService.removeById(id);
        }
    }
}
